//
//  SystemContentViewController.m
//  OutesIOS
//
//  Created by norman on 2019/1/5.
//  Copyright © 2019 norman. All rights reserved.
//

#import "SystemContentViewController.h"
#import "AccountViewController.h"
#import "UserAuditViewController.h"
#import "AnnouncementViewController.h"
#import "DataStatisticsViewController.h"
#import "FeedBackViewController.h"
#import "UserProfileData.h"
#import <TTTAttributedLabel.h>
#import "BrowserViewController.h"
#import "ProjectManageViewController.h"
#import "SystemSettingViewController.h"
#import "PublishNoticeViewController.h"
#import "CompanyInfoViewController.h"

typedef NS_ENUM(NSInteger, TTTAttributedLabelType) {
    TTTAttributedLabelTypeNone,
    TTTAttributedLabelTypeUrl,
    TTTAttributedLabelTypePhone
};


@interface SystemContentViewController ()<TTTAttributedLabelDelegate,UITableViewDataSource,UITableViewDelegate> {
}

@property (nonatomic,strong) UITableView *tableView;

@end

@implementation SystemContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupViews];
}

#pragma mark - 初始化视图
- (void)setupViews{
    self.title = @"系统";
    
    UIView *footer = [self addTableViewFooter];
    footer.x = 0;
    [self.view addSubview:footer];
    
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:[UIView frameWithOutNavTab] style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:tableView];
        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        tableView;
    });
    self.tableView.height -= footer.height;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.backgroundColor = CM_WINDOW_BG_COLOR;
    footer.y = CGRectGetMaxY(self.tableView.frame);
    //添加tableviewHeader
    //    self.tableView.tableHeaderView = [self addTableViewHeader];
    //    self.tableView.tableFooterView = [self addTableViewFooter];
}

- (UIView *)addTableViewHeader{
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height/5)];
    
    //ICON
    UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appicon"]];
    icon.contentMode = UIViewContentModeScaleAspectFit;
    icon.centerX = headView.centerX;
    icon.centerY = headView.centerY - 5;
    [headView addSubview:icon];
    
    //title
    NSString *version = [NSString stringWithFormat:@"版本：V%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleVersionKey]];
    UILabel *label = [UILabel labelSingleLineFontSize:kListDetailFontSize content:version];
    label.centerX = self.view.width*0.5;
    label.y = CGRectGetMaxY(icon.frame)+10;
    //居中
    label.textColor = CM_BLUE_COLOR;
    [headView addSubview:label];
    
    return headView;
}

- (UIView *)addTableViewFooter{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 0)];
    
    //美的官网
    CGSize size = [Tools sizeOfSingleLineText:@"中广欧特斯官网" withSize:kListDetailFontSize];
    CGSize size1 = [Tools sizeOfSingleLineText:@"优能易创官网" withSize:kListDetailFontSize];
    NSInteger w = size.width + size1.width + 8*CELL_CONTENT_MARGIN;
    TTTAttributedLabel *mideaLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(kScreen_Width/2 - w/2, CELL_PADDING_TOP, size.width, size.height)];
    [self fillDataToLabel:mideaLabel content:@"中广欧特斯官网" url:[NSURL URLWithString:@"http://www.midea.com/"] type:TTTAttributedLabelTypeUrl];
    mideaLabel.font = [UIFont systemFontOfSize:kListDetailFontSize];
    [footerView addSubview:mideaLabel];
    
    //优能易创官网
    TTTAttributedLabel *ynycLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(mideaLabel.frame) + 8*CELL_CONTENT_MARGIN, CELL_PADDING_TOP, size1.width, size1.height)];
    [self fillDataToLabel:ynycLabel content:@"优能易创官网" url:[NSURL URLWithString:@"http://www.hvacapp.com.cn/"] type:TTTAttributedLabelTypeUrl];
    ynycLabel.font = [UIFont systemFontOfSize:kListDetailFontSize];
    [footerView addSubview:ynycLabel];
    
    
    UILabel *support = [UILabel labelSingleLineFontSize:10 content:@"本软件技术支持由北京优能易创软件技术有限公司提供"];
    support.centerX = kScreen_Width/2;
    support.y = CGRectGetMaxY(ynycLabel.frame) + 5;
    support.textColor = CM_TABLE_DETAIL_COLOR;
    [footerView addSubview:support];
    
    UILabel *copyright = [UILabel labelSingleLineFontSize:10 content:@"Copyright© 2009-2015 All Rights Reserved"];
    copyright.centerX = kScreen_Width/2;
    copyright.y = CGRectGetMaxY(support.frame) + 5;
    copyright.textColor = CM_TABLE_DETAIL_COLOR;
    [footerView addSubview:copyright];
    
    NSString *version = [NSString stringWithFormat:@"当前版本号：V%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleVersionKey]];
    UILabel *label = [UILabel labelSingleLineFontSize:10 content:version];
    label.centerX = self.view.width*0.5;
    label.y = CGRectGetMaxY(copyright.frame) + 5;
    label.textColor = CM_TABLE_DETAIL_COLOR;
    [footerView addSubview:label];
    
    footerView.height = CGRectGetMaxY(label.frame) + CELL_PADDING_BOTTOM;
    
    return footerView;
}


#pragma mark - TableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 6;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Mine_Cell_Identifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.textColor = CM_TABLE_TITLE_COLOR;
    cell.textLabel.font = [UIFont systemFontOfSize:kListTitleFontSize];
    
    if (indexPath.section == 0){
        cell.imageView.image = [UIImage imageNamed:@"mine_user_approve"];
        cell.textLabel.text = @"中广欧特斯";
    } else if (indexPath.section == 1){
        cell.imageView.image = [UIImage imageNamed:@"mine_user_approve"];
        cell.textLabel.text = @"意见反馈";
    } else if (indexPath.section == 2){
        cell.imageView.image = [UIImage imageNamed:@"mine_project_approve"];
        cell.textLabel.text = @"公告查看";
    } else if (indexPath.section == 3){
        cell.imageView.image = [UIImage imageNamed:@"mine_statistics_data"];
        cell.textLabel.text = @"功能介绍";
    } else if (indexPath.section == 4){
        cell.imageView.image = [UIImage imageNamed:@"mine_notice_manager"];
        cell.textLabel.text = @"系统设置";
    } else{
        cell.imageView.image = [UIImage imageNamed:@"mine_loginout"];
        cell.textLabel.text = @"注销";
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0){
        [MBProgressHUD showSuccess:@"功能开发中，敬请期待。。。"];
        //中广欧特斯
//        CompanyInfoViewController *vc = [CompanyInfoViewController new];
//        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.section == 1){
        //意见反馈
        [MBProgressHUD showSuccess:@"功能开发中，敬请期待。。。"];
//        PublishNoticeViewController *vc = [PublishNoticeViewController new];
//        vc.publishType = KKPublish_Suggestion;
//        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.section == 2){
        //公告查看
        [MBProgressHUD showSuccess:@"功能开发中，敬请期待。。。"];
    } else if (indexPath.section == 3){
        //功能介绍
        [MBProgressHUD showSuccess:@"功能开发中，敬请期待。。。"];
//        BrowserViewController *view = [BrowserViewController new];
//        view.title = @"功能介绍";
//        view.urlStr = HTTP_FUNCTIONARY_INTRODUCE;
//        [self.navigationController pushViewController:view animated:YES];
    } else if (indexPath.section == 4){
        //参数设置
        [self.navigationController pushViewController:[SystemSettingViewController new] animated:YES];
    } else {
        //注销用户
        [[AccountManager manager] logoutAndClearUserInfo];
        [self showLoginViewController];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

#pragma mark -链接文本处理
/**
 *  填充数据到UILabel
 *
 *  @param label   UILabel
 *  @param content 内容
 */
- (void)fillDataToLabel:(TTTAttributedLabel *)label content:(NSString *)content url:(NSURL *)url type:(TTTAttributedLabelType)type{
    NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc] initWithString:content];
    [contentStr addAttributes:@{(__bridge NSString *)kCTForegroundColorAttributeName:CM_BLUE_COLOR} range:NSMakeRange(0, contentStr.length)];
    
    label.linkAttributes = kLinkAttributes;
    label.activeLinkAttributes = kLinkAttributesActive;
    label.font = [UIFont boldSystemFontOfSize:13];
    
    label.text = contentStr;
    label.textAlignment = NSTextAlignmentCenter;
    switch (type) {
        case TTTAttributedLabelTypePhone:{
            NSRange range = [label.text rangeOfString:content];
            [label addLinkToPhoneNumber:content withRange:range];
            label.delegate = self;
            break;
        }
        case TTTAttributedLabelTypeUrl:{
            NSRange range = [label.text rangeOfString:content];
            [label addLinkToURL:url withRange:range];
            label.delegate = self;
            break;
        }
        default:
            break;
    }
}

#pragma mark - TTAttributeLabel 代理
-(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url{
    [[UIApplication sharedApplication] openURL:url];
}

-(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneNumber]]];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
